<template>
  <div class="register">
    <div class="avatar">
      <van-icon name="user-circle-o" class="icon" />
    </div>
    <div class="user_info">
      <van-form @submit="onSubmit" :colon="true" :label-align="'right'" :label-width="'4rem'">
        <van-field
          v-model="email"
          name="email"
          label="邮箱"
          placeholder="请输入邮箱"
          :rules="[{ required: true, message: '请填写邮箱' }]"
        />
        <van-field
          v-model="username"
          name="username"
          label="昵称"
          placeholder="请输入昵称"
          :rules="[{ required: true, message: '请填写昵称' }]"
        />
         <van-field
          v-model="constellation"
          name="constellation"
          label="星座"
          placeholder="请输入星座"
        />
        <van-field
          v-model="profession"
          name="profession"
          label="职业"
          placeholder="请输入职业"
        />
        <van-field
          v-model="password"
          type="password"
          name="password"
          label="密码"
          placeholder="密码"
          :rules="[{ required: true, message: '请填写密码' }]"
        />
        <van-field name="radio" label="性别">
          <template #input>
            <van-radio-group v-model="radio" direction="horizontal">
              <van-radio name="1">男</van-radio>
              <van-radio name="2">女</van-radio>
            </van-radio-group>
          </template>
        </van-field>
        <div style="margin: 16px;">
          <van-button round block type="info" native-type="submit">注册</van-button>
        </div>
      </van-form>
    </div>
    <p>
      没有账号，现在去
      <router-link :to="{name: 'Login'}" class="login">登录</router-link>
    </p>
  </div>
</template>

<script>
export default {
  name: 'Register',
  data () {
    return {
      email: '',
      username: '',
      password: '',
      radio: '1',
      constellation: '',
      profession: ''
    }
  },
  methods: {
    onSubmit (userInfo) {
      this.$axios
        .post('/api/user/register', {
          email: this.email,
          username: this.username,
          password: this.password,
          gender: this.radio,
          constellation: this.constellation,
          profession: this.profession
        })
        .then(res => {
          // 注册成功
          this.$toast.success('注册成功')
          // 跳转到登录页面
          this.$router.push('/login')
        }, rej => {
          // 注册失败
          this.$toast.fail('注册失败')
          return false
        })
    }
  }
}
</script>

<style lang="stylus" scoped>
.register {
  display: flex;
  flex-direction: column;
  align-items: center;

  .avatar {
    margin-top: 5rem;
    margin-bottom: 2rem;
    width: 5rem;
    height: 5rem;

    .icon {
      width: 100%;
      height: 100%;
      border-radius: 50% 50%;
      text-align: center;
      font-size: 5rem;
      color: #cccccc;
    }
  }

  p {
    font-size: 0.8rem;

    .login {
      color: blue;
    }
  }
}
</style>
